import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class GiveHistoryDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;


    public static async insert_give_history(param: object) {
        let sender: number = param["sender"]
        let reciver: number = param["reciver"]
        let type: string = param["type"]
        let num: number = param["num"]
        let time: string = param["time"]
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = 'INSERT INTO t_give_history(sender,reciver,type,num,time) VALUES(?,?,?,?,?) ;';
        let sqlParams = [sender, reciver, type, num, time];
        client.queryPromise(sql, sqlParams);
    }

    public static async get_give_history(sender: number, reciver: number, cur_page: number, page_size:number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let startIndex: number = (cur_page - 1) * page_size;
        let sql: string = "select * from t_give_history where sender=? and reciver=?  limit ?,?"
        let sqlParams = [sender, reciver, startIndex, page_size];
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("get_give_history==", sql, sqlParams)
        if (rows && rows.length) {
            return rows
        }
        return []
    }

    public static async get_give_history_totalnum(sender: number, reciver: number) {
        let sql: string = "select count(*) from t_give_history where sender=? and reciver=?"
        let sqlParams: Array<any> = [sender, reciver];
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("get_give_history_totalnum==",  rows)
        if (rows) {
            return rows[0]["count(*)"]
        }
        return 0
    }

    public static async clear(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_give_history where time < ?"
        let sqlParams = [DateUtil.formatDateStr3(delete_time)];
        await client.queryPromise(sql, sqlParams);
    }



}